//
//  HTFrameConvert.m
//  HTERPManApp
//
//  Created by standychen on 14-11-7.
//  Copyright (c) 2014年 HT. All rights reserved.
//
//数据转换类
#import "HTFrameConvert.h"

@implementation HTFrameConvert


//转换Json字符串格式为日期 OC 日期字符串格式
+(NSString *)convertdateStrWithJsonStr:(NSString *)str andWithFormat:(NSString *)format
{
    NSDate *ndate=[self convertDateWithStr:str andWithFormat:@"yyyy-MM-dd"];
    
    NSString *reStr=[self convertStrWithDate:ndate andWithFormat:format];
    
    return reStr;
}


//转换日期为字符串格式
+(NSString *)convertStrWithDate:(NSDate *)date andWithFormat:(NSString *)format
{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat =format;
    return [df stringFromDate:date];
    
}

//转换字符串为标准日期
+(NSDate *)convertDateWithStr:(NSString *)str andWithFormat:(NSString *)format
{
    NSMutableString *strWaitProc=[[NSMutableString alloc] initWithString:str];
    NSString *strWaitConvert=[[NSString alloc] initWithString:str];
    if([str length]>[format length])
    {
        strWaitConvert=[strWaitProc substringToIndex:[format length]];
    }
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat =format;
    NSDate *reDate=[df dateFromString:strWaitConvert];
    
    return reDate;
}



//RGB字符串转换为颜色
+(UIColor *)convertColorWithColor:(NSString *)str
{
    UIColor *ucolor=[UIColor whiteColor];
    if ((NSNull *)str == [NSNull null]) {//如果传入的是null
        return ucolor;
    }
    
    
    NSArray *aArray = [str componentsSeparatedByString:@","];
    if([aArray count]>=3)
    {
        double redint= [self convertIntWithID:[aArray objectAtIndex:0]]/255.0;
        double greenint= [self convertIntWithID:[aArray objectAtIndex:1]]/255.0;
        double blueint= [self convertIntWithID:[aArray objectAtIndex:2]]/255.0;
        //int
        
        ucolor=[[UIColor alloc]initWithRed:redint green:greenint blue:blueint alpha:100];
    }
    return ucolor;
}



//id类型转换为int型
+(int)convertIntWithID:(id)context
{
    int ri=0;
    if ((NSNull *)context != [NSNull null])//不为NULL
    {
        NSString *str=[[NSString alloc] initWithFormat:@"%@",context];
        
        ri=[str intValue];
    }
    return ri;
}


//id类型转换为double型
+(int)convertDoubleWithID:(id)context
{
    double ri=0;
    if ((NSNull *)context != [NSNull null])//不为NULL
    {
        NSString *str=[[NSString alloc] initWithFormat:@"%@",context];
        
        ri=[str doubleValue];
    }
    return ri;
}

//id类型转换为nsstring型
+(NSString *)convertNSStringWithID:(id)context
{
    NSString *str=[[NSString alloc]init];
    if ((NSNull *)context != [NSNull null])//不为NULL
    {
        if ( [context isKindOfClass:[NSString class]] ) {
            str = context;
        }
    }
    return str;
}

+(int)getNowYear{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int year = [dateComponent year];
    
    return year;
}

+(int)getNowMonth{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int month = [dateComponent month];
    
    return month;
}

+(int)getNowDay{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int day = [dateComponent day];
    
    return day;
}

+(int)getNowHour{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int hour = [dateComponent hour];
    
    return hour;
}

+(int)getNowMin{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int min = [dateComponent minute];
    
    return min;
}

+(int)getNowSec{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int sec = [dateComponent second];
    
    return sec;
}

@end
